package com.vancouver.knows.ui.fragment.citydiscount;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.ui.refresh.RefreshControllerView;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.view.PullScrollView;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.UserInfo;

import me.everything.android.ui.overscroll.IOverScrollStateListener;
import me.everything.android.ui.overscroll.IOverScrollUpdateListener;
import me.everything.android.ui.overscroll.VerticalOverScrollBounceEffectDecorator;
import me.everything.android.ui.overscroll.adapters.ScrollViewOverScrollDecorAdapter;

/**
 * 城事详情下拉界面
 * 
 * @author chenxunlin01
 * 
 */
public class CityDiscountPullDetailControllerView extends RefreshControllerView {

	private ATaskMark aTaskMark;
	private String postId;
	private IResultReceiver resultReceiver;
	private BusinessModule bModule;

	public CityDiscountPullDetailControllerView(Context context) {
		super(context);
	}

	public CityDiscountPullDetailControllerView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void init(String postId, IResultReceiver resultReceiver) {
		this.postId = postId;
		this.resultReceiver = resultReceiver;
		bModule = ((MainApplication) imContext).getBusinessModule();
		aTaskMark = getATaskMark();
		((PullScrollView) content).init(aTaskMark);
	}

	private ATaskMark getATaskMark() {
		return bModule.getTaskMarkPool().getFetchDiscountDetailTaskMark(postId);
	}

	@Override
	public void onRefresh(ARefreshViewController controller) {
		aTaskMark = getATaskMark();
		((PullScrollView) content).init(aTaskMark);
		if (aTaskMark != null && aTaskMark.getTaskStatus() != ATaskMark.HANDLE_DOING) {
			aTaskMark.reinitTaskMark();
			aTaskMark.setTaskType(ATaskMark.TYPE_REFRESH);
			loadData();
		}
	}

	public void loadData() {
		UserInfo userInfo = ((MainApplication) imContext).getBusinessModule().getCacheManager().getUserInfo();
		String userId = userInfo == null ? "" : userInfo.getUserID();
		((MainApplication) imContext).getBusinessModule().getServiceWrapper()
				.fetchDiscountDetail(resultReceiver, aTaskMark, postId, userId, ((MainApplication) imContext).latitude, ((MainApplication) imContext).longitude);
	}

	@Override
	protected View createContentView() {
		PullScrollView scrollView = (PullScrollView) LayoutInflater.from(getContext()).inflate(
				R.layout.view_discount_detail_main_content, null);
		return scrollView;
	}

	public void setOverScroll(Object listener) {
		ScrollViewOverScrollDecorAdapter overScrollAdapter = new ScrollViewOverScrollDecorAdapter((PullScrollView) content);
		overScrollAdapter.isInAbsoluteEnd();
		VerticalOverScrollBounceEffectDecorator overScrollDecorator = new VerticalOverScrollBounceEffectDecorator(overScrollAdapter, 1f, 2f, -1f);
		overScrollDecorator.setOverScrollUpdateListener((IOverScrollUpdateListener) listener);
		overScrollDecorator.setOverScrollStateListener((IOverScrollStateListener) listener);
	}

}
